define(['jquery', 'bootstrap', 'backend', 'table', 'form', 'editable'], function ($, undefined, Backend, Table, Form, undefined) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'video/index' + location.search,
                    add_url: 'video/add',
                    edit_url: 'video/edit',
                    del_url: 'video/del',
                    multi_url: 'video/multi',
                    import_url: 'video/import',
                    table: 'video',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {
                            field: 'title',
                            title: __('Title'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'category_id',
                            title: '分类标题',
                            searchList: Config.category_list,
                            formatter: function (value, row, index) {
                                return row['category']['title'];
                            }
                        },
                        {
                            field: 'thumb',
                            title: __('Thumb'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.images,
                            events: Table.api.events.image,
                        },
                        {field: 'intro', title: __('Intro'), operate: 'LIKE'},
                        {field: 'year', title: __('Year')},
                        {field: 'duration', title: __('Duration'), operate: 'LIKE'},
                        {
                            field: 'desc',
                            title: __('Desc'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            formatter: Table.api.formatter.content
                        },
                        {
                            field: 'seo_keyword',
                            title: 'seo关键词',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            editable: {
                                type: 'text',
                                display: function (value, sourceData) {
                                    value = value.toString();
                                    if (value.length > 10) {
                                        value = value.substring(0, 10) + "...";
                                    }
                                    $(this).html(value);
                                },
                            }
                        },
                        {
                            field: 'seo_description',
                            title: 'seo描述',
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                            editable: {
                                type: 'text',
                                display: function (value, sourceData) {
                                    value = value.toString();
                                    if (value.length > 10) {
                                        value = value.substring(0, 10) + "...";
                                    }
                                    $(this).html(value);
                                },
                            }
                        },
                        {
                            field: 'creator',
                            title: __('Creator'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                        },
                        {
                            field: 'created_at',
                            title: __('Created_at'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'updater',
                            title: __('Updater'),
                            operate: 'LIKE',
                            table: table,
                            class: 'autocontent',
                        },
                        {
                            field: 'updated_at',
                            title: __('Updated_at'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            autocomplete: false
                        },
                        {
                            field: 'operate',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            formatter: Table.api.formatter.operate,
                            buttons: [
                                {
                                    name: 'channel',
                                    title: '添加渠道',
                                    text: '添加渠道',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'video_channel/add?video_id={ids}',
                                },
                                {
                                    name: 'channel_index',
                                    title: '渠道列表',
                                    text: '渠道列表',
                                    classname: 'btn btn-xs btn-success btn-dialog',
                                    url: 'video_channel/index?video_id={ids}',
                                    extend: "data-area='[\"90%\",\"90%\"]'",
                                }
                            ]
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
